
#include "fpga.h"

#include "stdio.h"


//########################################################################################################################################
//@ 
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
static void FPGA_SPI1_Init(void)
{
	SPI_InitPara SPI_InitStruct;
	GPIO_InitPara GPIO_InitStructure;

	RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOA | RCC_AHBPERIPH_GPIOB, ENABLE);
	RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_SPI1, ENABLE);

	GPIO_PinAFConfig(SPI1_IO,GPIO_PINSOURCE3,GPIO_AF_0);
	GPIO_PinAFConfig(SPI1_IO,GPIO_PINSOURCE4,GPIO_AF_0);
	GPIO_PinAFConfig(SPI1_IO,GPIO_PINSOURCE5,GPIO_AF_0);
	GPIO_PinAFConfig(SPI1_NSS_IO, GPIO_PINSOURCE15, GPIO_AF_0);

	GPIO_InitStructure.GPIO_Pin = SPI1_SCK | SPI1_MISO | SPI1_MOSI;
	GPIO_InitStructure.GPIO_Mode=GPIO_MODE_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
	GPIO_InitStructure.GPIO_Speed=GPIO_SPEED_50MHZ;
	GPIO_Init(SPI1_IO,&GPIO_InitStructure);



	GPIO_InitStructure.GPIO_Pin = SPI1_NSS;
	GPIO_InitStructure.GPIO_Mode=GPIO_MODE_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_SPEED_50MHZ;
	GPIO_Init(SPI1_NSS_IO,&GPIO_InitStructure);
	GPIO_SetBits(SPI1_NSS_IO, SPI1_NSS);

	SPI_InitStruct.SPI_Mode = SPI_MODE_MASTER;
	SPI_InitStruct.SPI_FirstBit = SPI_FIRSTBIT_MSB;
	SPI_InitStruct.SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX;
	SPI_InitStruct.SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT;
	SPI_InitStruct.SPI_SCKPH = SPI_SCKPH_2EDGE;
	SPI_InitStruct.SPI_SCKPL = SPI_SCKPL_LOW;
	SPI_InitStruct.SPI_SWNSSEN = SPI_SWNSS_SOFT;
	SPI_InitStruct.SPI_PSC = SPI_PSC_2;
	SPI_InitStruct.SPI_CRCPOL = 7;
	SPI_Init(SPI1, &SPI_InitStruct);

	SPI_CRC_Enable(SPI1, ENABLE);
	SPI_Enable(SPI1,ENABLE);	
}
//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
static u8 FPGA_SPI1_Read_Byte(u8 byte)
{
	u8 temp;
	GPIO_ResetBits(SPI1_NSS_IO,SPI1_NSS);
	__nop();
	while(SPI_I2S_GetBitState(SPI1,SPI_FLAG_TBE)==RESET);
	SPI_I2S_SendData(SPI1,byte);
	while(SPI_I2S_GetBitState(SPI1,SPI_FLAG_RBNE)==RESET);
	temp = SPI_I2S_ReceiveData(SPI1);
	__nop();
	GPIO_SetBits(SPI1_NSS_IO,SPI1_NSS);	
	return temp;
}



//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
void FPGA_Reset(void)
{
	GPIO_ResetBits(FPGA_RST_IO, FPGA_RST);
	GPIO_SetBits(FPGA_RST_IO, FPGA_RST);
}

//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
void FPGA_GPIO_Init(void)
{
	GPIO_InitPara GPIO_InitStructure;
	RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOC | RCC_AHBPERIPH_GPIOF, ENABLE);

	GPIO_InitStructure.GPIO_Pin = FPGA_RST;//GPIO_PIN_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
	GPIO_Init(FPGA_RST_IO, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = FPGA_INT1;
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP;
	GPIO_Init(FPGA_INT1_IO, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = FPGA_INT2;
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP;
	GPIO_Init(FPGA_INT2_IO, &GPIO_InitStructure);
}

//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
void FPGA_SPI1_Send_Byte(u8 byte)
{
	GPIO_ResetBits(SPI1_NSS_IO,SPI1_NSS);
	__nop();
	while(SPI_I2S_GetBitState(SPI1,SPI_FLAG_TBE)==RESET);
	SPI_I2S_SendData(SPI1,byte);
	while(SPI_I2S_GetBitState(SPI1,SPI_FLAG_RBNE)==RESET);
	SPI_I2S_ReceiveData(SPI1);
	__nop();
	GPIO_SetBits(SPI1_NSS_IO,SPI1_NSS);	
}

//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
void FPGA_Data_Get(u8 command,u16 *fpga_adc_buf, u16 len)
{
	u16 i;
	u16 fpga_adc_data0, fpga_adc_data1, fpga_adc_data2;
	
	FPGA_SPI1_Send_Byte(command);	

	for(i = 0; i < len; i++)
	{
		while(GPIO_ReadInputBit(FPGA_INT1_IO, FPGA_INT1)== 0);
		
		FPGA_SPI1_Send_Byte(0x31);
		
		fpga_adc_data0 = (u16)FPGA_SPI1_Read_Byte(0x32);
		
		fpga_adc_data1 = (u16)FPGA_SPI1_Read_Byte(0x33);
		
		fpga_adc_data2 = (fpga_adc_data1 << 8) + fpga_adc_data0;

		fpga_adc_buf[i] = fpga_adc_data2;
		
		GPIO_ResetBits(FPGA_INT2_IO, FPGA_INT2);
		
		GPIO_SetBits(FPGA_INT2_IO, FPGA_INT2);
	}
	
	FPGA_Reset();
}


//****************************************************************************************************************************************
//@ 
//****************************************************************************************************************************************
void FPGA_Init(void)
{
	FPGA_SPI1_Init();
	
	FPGA_GPIO_Init();
	
	FPGA_Reset();
}

